﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-row :gutter="10">
        <a-col :md="20" :sm="24">
          <a-button v-if="hasPerm('PB_Supplier.Add')" type="primary" icon="plus" @click="hanldleAdd()">新建</a-button>
          <a-button
            v-if="hasPerm('PB_Supplier.Delete')"
            type="primary"
            icon="minus"
            @click="handleDelete(selectedRowKeys)"
            :disabled="!hasSelected()"
            :loading="loading"
          >删除</a-button
          >
          <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
        </a-col>
<!--        <a-button v-if="hasPerm('PB_Supplier.Leading')" type="primary" @click="hanldleLeading()">导入供货单位</a-button>-->
      </a-row>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <!-- <a-col :md="6" :sm="24">
            <a-form-item>
              <a-form-item label="查询类别">
                <a-select allowClear v-model="queryParam.Condition" placeholder="查询类别">
                  <a-select-option key="Code">供货单位编号</a-select-option>
                  <a-select-option key=" Name">供货单位名称</a-select-option>
                  <a-select-option key="TypeNmae">供货单位类型</a-select-option>
                </a-select>
              </a-form-item>
            </a-form-item>
          </a-col> -->
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.KeyWord" placeholder="供货单位编号/供货单位名称/供货单位类型/联系人" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
            >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="Reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
      :scroll="{ y: 560 }"
    >
      <template slot="States" slot-scope="text">
        {{ States(text) }}
      </template>
      <span slot="action" slot-scope="text, record">
        <template>
          <a
            v-if="hasPerm('PB_Supplier.Deactivate') && record.States == 1"
            @click="Updatestatefalse(record.Id)"
          >停用</a
          >
          <a-divider v-if="hasPerm('PB_Supplier.Delete') && record.States == 1" type="vertical" />
          <a v-if="hasPerm('PB_Supplier.Enable') && record.States == 0" @click="UpdateState(record.Id)">启用</a>
          <a-divider v-if="hasPerm('PB_Supplier.Delete') && record.States == 0" type="vertical" />
          <a v-if="hasPerm('PB_Supplier.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('PB_Supplier.Delete') && record.States == 0" type="vertical" />
          <a v-if="hasPerm('PB_Supplier.Delete') && record.States == 0" @click="handleDelete([record.Id])">删除</a>
          <a-divider v-if="hasPerm('PB_Supplier.AddressList')" type="vertical" />
          <a v-if="hasPerm('PB_Supplier.AddressList')" @click="openAddressList(record.Id, record.Name)">地址</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
    <address-list ref="addressList" :parentObj="this"></address-list>
    <leading-show
      ref="leadingShow"
      :parentObj="this"
      leading="/PB/PB_Supplier/Import"
      templet="/PB/PB_Supplier/ExportToExcel"
    ></leading-show>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import AddressList from '../../PB/PB_Address/List'

const columns = [
  { title: '编号', dataIndex: 'Code' },
  { title: '名称', dataIndex: 'Name' },
  { title: '简称', dataIndex: 'Abbreviation' },
  { title: '类型', dataIndex: 'TypeNmae' },
  { title: '电话', dataIndex: 'Phone' },
  { title: 'Email', dataIndex: 'Email' },
  { title: '联系人', dataIndex: 'ContactName' },
  { title: '状态', dataIndex: 'States', scopedSlots: { customRender: 'States' } },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm,
    AddressList
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    Reset () {
      this.queryParam = {}
      this.getDataList()
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/PB/PB_Supplier/QueryDataListPB_Supplier', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    States (text) {
      console.log(text)
      var str = ''
      if (text === false) {
        str = '停用'
      } else if (text === true) {
        str = '启用'
      }
      return str
    },
    Updatestatefalse (id) {
      this.$http.post('/PB/PB_Supplier/Updatestatefalse?id=' + id, {}).then(res => {
        console.log(res)
        if (res.Success) {
          this.getDataList()
          this.$message.error('停用!')
        }
      })
    },
    UpdateState (id) {
      console.log(id)
      this.$http.post('/PB/PB_Supplier/UpdateState?id=' + id, {}).then(res => {
        console.log(res)
        if (res.Success) {
          this.getDataList()
          this.$message.success('启用!')
        }
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.editForm.openForm(null, '新增供货单位')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑供货单位')
    },
    hanldleLeading () {
      this.$refs.leadingShow.openForm(null, '导入供货单位信息')
    },
    handleDelete (ids) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          this.$http.post('/PB/PB_Supplier/DeleteData', ids).then(resJson => {
            resolve()

            if (resJson.Success) {
              this.$message.success('操作成功!')

              this.getDataList()
            } else {
              this.$message.error(resJson.Msg)
            }
          })
        }).catch(() => console.log('Oops errors!'))
      })
    },
    openAddressList (id, name) {
      this.$refs.addressList.openDrawer(false, id, name)
    }
  }
}
</script>
